<script setup>
import { ref, computed, onMounted } from 'vue'
import { RouterLink, RouterView } from 'vue-router'
import { getCurrentUser, isLoggedIn, logout } from './services/membershipService.js'

const currentUser = ref(null)
const showUserMenu = ref(false)

// 初始化用户状态
const initializeUser = () => {
  if (isLoggedIn()) {
    currentUser.value = getCurrentUser()
  }
}

// 切换用户菜单显示状态
const toggleUserMenu = () => {
  showUserMenu.value = !showUserMenu.value
}

// 退出登录
const handleLogout = () => {
  logout()
  currentUser.value = null
  showUserMenu.value = false
}

// 生成随机头像（使用ui-avatars.com提供的服务）
const getRandomAvatar = (username) => {
  // 使用用户名生成随机颜色
  const colors = ['FF6B6B', '4ECDC4', '45B7D1', '96CEB4', 'FECA57', 'DDA0DD', '98D8C8', 'F8B195']
  const nameHash = username.split('').reduce((acc, char) => acc + char.charCodeAt(0), 0)
  const colorIndex = nameHash % colors.length
  const bgColor = colors[colorIndex]
  
  return `https://ui-avatars.com/api/?name=${encodeURIComponent(username)}&background=${bgColor}&color=fff&size=40`
}

// 组件挂载时初始化用户状态
onMounted(() => {
  initializeUser()
})
</script>

<template>
  <header>
    <div class="wrapper">
      <nav>
        <RouterLink to="/">动物乐园</RouterLink>
        
        <!-- 如果用户已登录，显示头像、会员等级和个人中心 -->
        <template v-if="currentUser">
          <!-- 用户信息显示 -->
          <div class="user-info-display" @click="toggleUserMenu">
            <!-- 头像 -->
            <div class="user-avatar-container">
              <img 
                :src="getRandomAvatar(currentUser.username)" 
                :alt="currentUser.username"
                class="user-avatar"
              >
            </div>
            
            <!-- 用户名称 -->
            <span class="user-display-name">{{ currentUser.username }}</span>
            
            <!-- 会员等级 -->
            <div 
              class="membership-level-badge"
              :style="{
                backgroundColor: currentUser.membership?.color || '#cd7f32'
              }"
            >
              {{ currentUser.membership?.name || '普通会员' }}
            </div>
          </div>
          
          <!-- 个人中心链接 -->
          <RouterLink to="/membership" class="user-center-link">
            个人中心
          </RouterLink>
          
          <!-- 用户信息下拉菜单 -->
          <div v-if="showUserMenu" class="user-dropdown-menu">
            <div class="user-info">
              <div class="user-name">{{ currentUser.username }}</div>
              <div class="user-email">{{ currentUser.email }}</div>
              <div v-if="currentUser.membership" class="user-membership">
                {{ currentUser.membership.name }}
              </div>
            </div>
            <div class="dropdown-divider"></div>
            <button class="dropdown-item" @click="handleLogout">
              退出登录
            </button>
          </div>
        </template>
        
        <!-- 如果用户未登录，显示登录、注册和会员中心按钮 -->
        <template v-else>
          <RouterLink to="/login">登录</RouterLink>
          <RouterLink to="/register">注册</RouterLink>
          <RouterLink to="/membership">会员中心</RouterLink>
        </template>
      </nav>
    </div>
  </header>

  <RouterView />
</template>

<style scoped>
header {
  line-height: 1.5;
  width: 100%;
}

nav {
  width: 100%;
  font-size: 12px;
  text-align: center;
  margin-top: 2rem;
  position: relative;
}

nav a.router-link-exact-active {
  color: var(--color-text);
}

nav a.router-link-exact-active:hover {
  background-color: transparent;
}

nav a {
  display: inline-block;
  padding: 0 1rem;
  border-left: 1px solid var(--color-border);
}

nav a:first-of-type {
  border: 0;
}

/* 登录后的用户信息显示 */
.user-info-display {
  display: inline-flex;
  align-items: center;
  margin: 0 0.5rem;
  cursor: pointer;
  position: relative;
  padding: 0.5rem 1rem;
  border-radius: 8px;
  transition: background-color 0.2s ease;
}

.user-info-display:hover {
  background-color: var(--color-background-soft);
}

.user-avatar-container {
  margin: 0 0.5rem 0 0;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  transition: transform 0.2s ease;
}

.user-avatar:hover {
  transform: scale(1.1);
}

.user-display-name {
  font-weight: 500;
  color: var(--color-text);
  margin-right: 0.5rem;
  font-size: 14px;
}

.membership-level-badge {
  display: inline-block;
  padding: 0.25rem 0.75rem;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: bold;
  color: white;
  height: 24px;
  line-height: 24px;
  vertical-align: middle;
}

.user-center-link {
  display: inline-block;
  padding: 0 1rem;
  border-left: 1px solid var(--color-border);
}

/* 用户头像下拉菜单样式 */
.user-dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 5px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  padding: 10px 0;
  min-width: 200px;
  z-index: 1000;
}

.user-info {
  padding: 10px 15px;
  border-bottom: 1px solid #eee;
}

.user-name {
  font-weight: bold;
  color: #333;
  margin-bottom: 4px;
}

.user-email {
  font-size: 12px;
  color: #666;
  margin-bottom: 4px;
}

.user-membership {
  font-size: 12px;
  color: #4CAF50;
  font-weight: 500;
}

.dropdown-divider {
  height: 1px;
  background-color: #eee;
  margin: 5px 0;
}

.dropdown-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px 15px;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 14px;
  color: #333;
  transition: background-color 0.2s ease;
}

.dropdown-item:hover {
  background-color: #f5f5f5;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }

  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
    width: 100%;
  }

  nav {
    text-align: left;
    margin-left: -1rem;
    font-size: 1rem;

    padding: 1rem 0;
    margin-top: 1rem;
  }
}
</style>
